﻿using JsonConversion;
using Newtonsoft.Json;
using RTracker.Models.Common;
using RTracker.Models.ErrorLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace RTracker.Areas.Recruitment.Models.CandidateSource
{
    public class MCandidateSource
    {
        /// static variable RowSize from config settings to get list of this row size.
        public static int nRowSize = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRecordsInListCalling"]);

        /// <summary>
        /// This method is used to create a CandidateSource in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <returns>CStatus which is an object that contain transaction details</returns>
        public CStatus CreateCandidateSource(CAuthenticationContext objCAuthenticationContext, ref CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_create_candidate_source_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_source_name", SqlDbType.NVarChar).Value = objCCandidateSource.CandidateSourceName;
                    cmd.Parameters.Add("@candidate_source_desc", SqlDbType.NVarChar).Value = objCCandidateSource.CandidateSourceDesc;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to delete a CandidateSource in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus DeleteCandidateSource(CAuthenticationContext objCAuthenticationContext, ref CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_delete_candidate_source_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@candidate_source_ids", SqlDbType.NVarChar).Value = objCCandidateSource.CandidateSourceIds;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to update the CandidateSource detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus UpdateCandidateSource(CAuthenticationContext objCAuthenticationContext, ref CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_update_candidate_source_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@candidate_source_id", SqlDbType.Int).Value = objCCandidateSource.CandidateSourceIds;
                    cmd.Parameters.Add("@candidate_source_name", SqlDbType.NVarChar).Value = objCCandidateSource.CandidateSourceName;
                    cmd.Parameters.Add("@candidate_source_desc", SqlDbType.NVarChar).Value = objCCandidateSource.CandidateSourceDesc;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCCandidateSource.CreatedBy;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to get the CandidateSource detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource .</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetCandidateSourceDetail(CAuthenticationContext objCAuthenticationContext, ref CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_candidate_source_detail_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@candidate_source_id", SqlDbType.Int).Value = objCCandidateSource.CandidateSourceId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        objCCandidateSource = dt.ConvertToListFromDataTableCandidateSource()[0];
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.DetailSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCandidateSourceSoftLock(CAuthenticationContext objCAuthenticationContext, CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_candidate_source_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_source_id", SqlDbType.Int).Value = objCCandidateSource.CandidateSourceId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetCandidateSourceHardLock(CAuthenticationContext objCAuthenticationContext, CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_candidate_source_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_source_id", SqlDbType.Int).Value = objCCandidateSource.CandidateSourceId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Releases the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCandidateSourceSoftLock(CAuthenticationContext objCAuthenticationContext, CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_candidate_source_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_source_id", SqlDbType.Int).Value = objCCandidateSource.CandidateSourceId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Releases the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about user's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseCandidateSourceHardLock(CAuthenticationContext objCAuthenticationContext, CCandidateSource objCCandidateSource)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_candidate_source_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_source_id", SqlDbType.Int).Value = objCCandidateSource.CandidateSourceId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCCandidateSource.CreatedBy;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        ///  This function is used to get complete CandidateSource list
        /// </summary>
        /// <returns>List of type CCandidateSource</returns>
        public GCandidateSource GetCandidateSourceGridList(CCandidateSource objCCandidateSource, CAuthenticationContext objCAuthenticationContext)
        {
            GCandidateSource objGCandidateSource = new GCandidateSource();
            int nTotalRecords = 0;
            int nCurrentRowSize = nRowSize;

            if (objCCandidateSource.RowSize != 0)
            {
                nCurrentRowSize = objCCandidateSource.RowSize;
            }
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_candidate_source_paging_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@candidate_source_id", SqlDbType.Int).Value = objCCandidateSource.CandidateSourceId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCCandidateSource.OrganizationId;
                    cmd.Parameters.Add("@page_number", SqlDbType.Int).Value = objCCandidateSource.PageNumber;
                    cmd.Parameters.Add("@row_size", SqlDbType.Int).Value = nCurrentRowSize;
                    cmd.Parameters.Add("@sort_on", SqlDbType.VarChar).Value = objCCandidateSource.SortOn;
                    cmd.Parameters.Add("@sort_type", SqlDbType.VarChar).Value = objCCandidateSource.SortType;
                    cmd.Parameters.Add("@search", SqlDbType.VarChar).Value = objCCandidateSource.Search;
                    cmd.Parameters.Add("@searchon", SqlDbType.VarChar).Value = objCCandidateSource.SearchOn;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    objGCandidateSource.Paging = new CPaging();
                    if (rowsAffected > 0)
                    {
                        objGCandidateSource.LstCandidateSource = dt.ConvertToListFromDataTableCandidateSource();
                        objGCandidateSource.LstCandidateSource[0].PageNumber = int.Parse(cmd.Parameters["@idOut"].Value.ToString());
                        objGCandidateSource.Paging.PageNumber = objGCandidateSource.LstCandidateSource[0].PageNumber;
                    }
                    else
                    {
                        objGCandidateSource.LstCandidateSource = null;
                    }


                    nTotalRecords = int.Parse(cmd.Parameters["@error"].Value.ToString());

                    objGCandidateSource.Status = new CStatus();

                    if (nTotalRecords < 0)
                    {
                        objGCandidateSource.Status.Success = false;
                        objGCandidateSource.Status.StatusDetails = nTotalRecords.DbError();
                    }
                    else
                    {
                        objGCandidateSource.Paging.TotalRecordsCount = nTotalRecords;
                        objGCandidateSource.Paging.PageSize = nCurrentRowSize;

                        objGCandidateSource.Status.Success = true;
                        objGCandidateSource.Status.StatusDetails = "success";
                    }
                }
                catch (Exception ex)
                {
                    objGCandidateSource.Status = new CStatus();
                    objGCandidateSource.Status.Success = false;
                    objGCandidateSource.Status.StatusDetails = ex.Message;
                }
            }
            return objGCandidateSource;
        }

        /// /// <summary>
        ///  This function is used to get complete CandidateSource list working under a same organization.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about CandidateSource's authentication context.</param>
        /// <param name="objCCandidateSource">This object contains details of the CandidateSource.</param>
        /// <param name="lstCCandidateSource">"out" the list of CandidateSources</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetCandidateSourceList(CAuthenticationContext objCAuthenticationContext, int organizationId, out List<CCandidateSource> lstCCandidateSource)
        {
            CStatus oCStatus = new CStatus();
            lstCCandidateSource = new List<CCandidateSource>();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_get_candidate_source_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = organizationId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    //cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    //cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        lstCCandidateSource.AddRange(dt.ConvertToListFromDataTableCandidateSource());
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

    }
}